package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-02-13
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private OssFileService ossFileService;

    @Override
    public IPage<Teacher> selectPage(Integer page, Integer limit, TeacherQuery teacherQuery) {
        Page<Teacher> pageParam = new Page<>(page , limit);
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");


        if(teacherQuery == null){
            return baseMapper.selectPage(pageParam , wrapper);
        }
        if (StringUtils.isNotBlank(teacherQuery.getName())){
            // 左%会使索引失效
            wrapper.likeRight("name" , teacherQuery.getName());
        }
        if (teacherQuery.getLevel() != null){
            wrapper.eq("level" , teacherQuery.getLevel());
        }
        if (StringUtils.isNotBlank(teacherQuery.getJoinDateBegin())){
            wrapper.ge("join_date" , teacherQuery.getJoinDateBegin());
        }
        if (StringUtils.isNotBlank(teacherQuery.getJoinDateEnd())){
            wrapper.le("join_date" , teacherQuery.getJoinDateEnd());
        }
        return this.baseMapper.selectPage(pageParam, wrapper);
    }

    @Override
    public List<Map<String, Object>> selectNameListByKey(String key) {

        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeLeft(Teacher::getName , "key");
        return baseMapper.selectMaps(wrapper);
    }

    @Override
    public Boolean removeAvatarById(String id) {
        Teacher teacher = baseMapper.selectById(id);
        if (teacher != null){
            if (StringUtils.isNotBlank(teacher.getAvatar())){
                // 删除讲师头像
                R r = ossFileService.removeFile(teacher.getAvatar());
                return r.getSuccess();
            }
        }
        return false;
    }

}
